// pages/userinfo/userinfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    viewId: 1,
    dx: 0,
    num: 0,
    time: "",
    subtract: 0,
    sex: '保密',
    userPlace: "",
    headerurl: '',
    userName: "",

    // 唱片展示数组
    arr1: [{
        title: "唱片展示",
        introduce: "表达音乐品味",
        last: '布置唱片架'
      },
      {
        title: "展示动态图片",
        introduce: "让别人了解你",
        last: '使用相册'
      },
      {
        title: "大声说出你的",
        introduce: "愿望",
        last: '布置唱片架'
      }
    ],
    // 创建歌单
    arr2: [{
      img: "../image/panda.jpg",
      title: '年度歌单',
      num: '10首'
    }, {
      img: "../image/panda.jpg",
      title: '年度歌单',
      num: '10首'
    }, ],

    // 收藏的歌单
    arr3: []
  },

  //关注
  attention() {
    wx.navigateTo({
      url: '../attention/attention',
    })
  },

  scroll(e) {
    ;
    if (e.detail.deltaX < 0) {
      this.setData({
        viewId: 2
      })
    } else {
      this.setData({
        viewId: 1
      })
    }
  },

  hotSwiperTransition: function (e) {
    if (e.detail.dx > 148) {
      this.setData({
        viewId: 2
      })
    }
    if (e.detail.dx <= 148) {
      this.setData({
        viewId: 1
      })
    }
  },

  navclick(e) {
    this.setData({
      viewId: e.currentTarget.dataset.idex
    })
  },

  navclick2(e) {
    this.setData({
      viewId: e.currentTarget.dataset.idex
    })
  },

  // 查看所有基本信息
  findAll() {
    wx.navigateTo({
      url: '../findAll/findAll',
    })
  },

   // 前往收藏歌单
   tosonglist(e){
    wx.navigateTo({
      url: `../details/details?id=${e.currentTarget.dataset.id}&songsheetCode=${e.currentTarget.dataset.songsheetcode}`})
  },

  // 前往个人歌单
  tomyself(e) {
    wx.navigateTo({
      url: `../personlist/personlist?id=${e.currentTarget.dataset.id}`
    })
  },

  // 点击添加个人简历
  resume() {
    wx.navigateTo({
      url: '../resume/resume',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 用户信息
    wx.request({
      url: 'http://192.168.0.26:8081/user/getUserByCode',
      method: 'post',
      data: {
        userCode: wx.getStorageSync('userCode'),
      },
      success: (res) => {
        var myDate = new Date();
        let arr = res.data.data
        let x = arr.createTime.split(" ")[0]
        let oldyear = arr.createTime.split("-")[0]
        let year = myDate.getFullYear()
        let subtract = year - oldyear
        // 判断性别
        if (arr.userGender == 2) {
          this.setData({
            sex: "女"
          })
        } else if (arr.userGender == 1) {
          this.setData({
            sex: "男"
          })
        } else {
          this.setData({
            sex: "保密"
          })
        }
        // 图片地址
        if (arr.headerurl != "" && arr.headerurl != null) {
          this.setData({
            headerurl: `http://192.168.0.26:8081${arr.headerurl}`
          })
        } else {
          this.setData({
            headerurl: '',
          })
        }
        // 判空
        if (arr.userPlace != "" && arr.userPlace != null) {
          this.setData({
            userPlace: arr.userPlace,
          })
        } else {
          this.setData({
            userPlace: "未填写"
          })
        }
        // 昵称
        this.setData({
          time: x,
          subtract: subtract,
          userName: arr.userName
        })
      }
    })

    // 用户关注
    wx.request({
      url: 'http://192.168.0.26:8081/userFollow/getAllFollowByUserCode',
      method: 'POST',
      data: {
        userCode: wx.getStorageSync('userCode'),
      },
      success: (res) => {
        let arr = res.data.data
        this.setData({
          num: arr.length
        })
      }
    })

    // 用户创建歌单
    wx.request({
      url: 'http://192.168.0.26:8081/songSheet/getSongSheetByUserId',
      method: "GET",
      data: {
        id: wx.getStorageSync('userid')
      },
      success: (res) => {
        for (let i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].imgUrl != null && res.data.data[i].imgUrl != "") {
            res.data.data[i].imgUrl = "http://192.168.0.26:8081" + res.data.data[i].imgUrl
          }
        }
        this.setData({
          arr2: res.data.data
        })
      }
    })
    // 用户收藏歌单
    wx.request({
      url: 'http://192.168.0.26:8081/userCollect/getUserCollectByUserCode',
      method: 'POST',
      data: {
        userCode: wx.getStorageSync('userCode'),
      },
      success: (res) => {
        let arr = res.data.data
        this.setData({
          arr3: arr
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})